package com.example.administrator.weipiao;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;

import adapter.FragmentAdapter;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private SharedPreferences sp;
    private ViewPager mVp;
    private ActionBarDrawerToggle mToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消标题栏
        //RequestWindowFeature(Window.FEATURE_NO_TITLE);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //用来判断是否是第一次启动的方法
        Splash();
        //控制滑动屏幕点击下面切换页面的方法
        viewPager();
        //设置drawerLayout里的内容,用来设置头部导航栏
        toolbarAndDrawerlayout();

    }
    //设置drawerLayout里的内容,用来设置头部导航栏
    private void toolbarAndDrawerlayout() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        assert drawerLayout != null;
        //抽屉的开关
        mToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
        //同步抽屉的状态
        mToggle.syncState();

        //填充菜单项
        toolbar.inflateMenu(R.menu.main);
        //处理菜单项的点击事件
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_search:
                        Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action_scan:
                        Toast.makeText(MainActivity.this, "动作片", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action_setting:
                        Toast.makeText(MainActivity.this, "科幻片", Toast.LENGTH_SHORT).show();
                        break;
                }
                return false;
            }
        });
    }

    //控制滑动屏幕点击下面切换页面的方法
    private void viewPager() {
        mVp = (ViewPager) findViewById(R.id.vp);
        final RadioGroup rdgWp = (RadioGroup) findViewById(R.id.rdg_wp);

        //设置适配器，以及为适配器创建数据源
        ArrayList<Fragment> fragments = new ArrayList<>();
        assert rdgWp != null;
        for (int i=0;i<rdgWp.getChildCount();i++){
            Fragment fragment = FragmentFactory.createFragment(i);
            fragments.add(fragment);
        }
        mVp.setAdapter(new FragmentAdapter(getSupportFragmentManager(),fragments));

        //ViewPager有一个预加载的特性，默认情况下，预先加载一个Fragment
        //默认加载一项，设置0，无效
        mVp.setOffscreenPageLimit(1);
        //当ViewPager滑动的时候，让底部的RadioGroup也跟着滑动
        mVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                ((RadioButton) rdgWp.getChildAt(position)).setChecked(true);
//                ((RadioGroup)rdgWp.getChildAt(position)).setOnCheckedChangeListener(true);
            }
        });
        //当点击RadioButton的时候，切换到对应的ViewPager的相应的Fragment中
        rdgWp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i=0;i<group.getChildCount();i++){
                    if (group.getChildAt(i).getId()==checkedId){
                        //处理点击事件
                        findViewById(checkedId).setTag(i);
                        findViewById(checkedId).setOnClickListener(MainActivity.this);
                    }
                }
            }
        });

    }

    //用来判断是否是第一次启动APP
    private void Splash() {
        sp = getSharedPreferences("content",MODE_PRIVATE);
        if (!sp.contains("firstStart")){
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("firstStart","aaaaaa");
            editor.apply();
            Intent intent = new Intent(this, SecondActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onClick(View v) {
        int which = (int) v.getTag();
        mVp.setCurrentItem(which);
    }
}
